#include "queue_cursor.h"

#include <boost/bind.hpp>

#include "interfaces/queue_interface.h"

using namespace pita;

QueueCursor::QueueCursor(QueueInterface& q) :
	queue_ (&q),
	position_ (0)
{
	q.eraseSignal.connect(
		boost::bind(&QueueCursor::erased, this, _1));
}

void QueueCursor::next()
{
	if (position() >= queue().size()) 
		return;
	
	++position_;

	changeSignal();
}

void QueueCursor::previous()
{
	if (position() == 0) return;

	--position_;

	changeSignal();
}

Index QueueCursor::position() const
{
	return position_;
}

void QueueCursor::setPosition(Index index)
{
	if (index > queue().size())
		return;

	position_ = index;

	changeSignal();
}

QueueInterface& QueueCursor::queue()
{
	return *queue_;
}

const QueueInterface& QueueCursor::queue() const
{
	return *queue_;
}

void QueueCursor::erased(Index index)
{
	// we only really care about this signal when
	// we're on the last element and it's the one
	// being erased.
	
	if (position() > queue().size())
		setPosition(queue().size());
}
